package com.airlines.system.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.airlines.system.dao.SysFunctionMapper;
import com.airlines.system.domain.SysFunction;
import com.airlines.system.domain.SysMenu;
import com.airlines.system.service.SysFunctionService;
import com.airlines.system.vo.MenuAndFuncTreeNode;


@Service
public class SysFunctionServiceImpl implements SysFunctionService {

    @Autowired
    private SysFunctionMapper sysFunctionMapper;

    @Override
    public List<SysFunction> findByRoleId(String roleId) {
        return sysFunctionMapper.findByRoleId(roleId);
    }


    @Override
    public List<MenuAndFuncTreeNode> makeTreeData(List<SysMenu> menuList, List<SysFunction> funcList) {
        List<MenuAndFuncTreeNode> result = new ArrayList<>();
        for (SysMenu menu : menuList) {
            if (menu.getMenuLevel() == 1) {
                result.add(new MenuAndFuncTreeNode(menu.getId(), menu.getMenuName(), true, true, "", menu.getMenuParentId(), true));
            }
            if (menu.getMenuLevel() == 2) {
                Boolean flag = false;
                for (SysFunction function : funcList) {
                    if (function.getMenuId().equals(menu.getId())) {
                        result.add(new MenuAndFuncTreeNode(function.getId(), function.getFuncName(), true, false, "", menu.getId(), false));
                        flag = true;
                    }
                }
                if (flag) {
                    result.add(new MenuAndFuncTreeNode(menu.getId(), menu.getMenuName(), true, true, "", menu.getMenuParentId(), true));
                } else {
                    result.add(new MenuAndFuncTreeNode(menu.getId(), menu.getMenuName(), true, true, "", menu.getMenuParentId(), false));
                }
            }
        }
        return result;
    }
    /* @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleTypeMapper sysRoleTypesMapper;
    @Autowired
    private SysRoleFuncMapper sysRoleFuncMapper;

    @Override
    public List<SysFunc> findAll(String typeId) {
        List<SysFunc> list = sysFuncMapper.findAll(typeId);
        list = setParentMenuName(list);
        return list;
    }

    @Override
    public Boolean addFunc(String typeId, SysFunc sysFunc) {
        //判断重复

        // save t_sys_function
        sysFunc.setId(UUID.randomUUID().toString());
        sysFunc.setStatus(1);
        return sysFuncMapper.insertSelective(sysFunc) > 0 ? true : false;
    }


    @Override
    public Map<String, List<SysFunc>> findTypeAndRoleFuncs(String roleId) {
        //得到系统的所有功能列表
        SysRoleType sysRoleType = sysRoleTypesMapper.findByRoleId(roleId);
        List<SysFunc> typeFunList = findAll(sysRoleType.getTypeId());
        //获取角色的权限功能列表
        List<SysFunc> roleList = sysFuncMapper.findByMenuId(roleId);
        roleList = setParentMenuName(roleList);
        Map<String, List<SysFunc>> map = new HashMap<>();
        map.put("type", typeFunList);
        map.put("role", roleList);
        return map;
    }

    private List<SysFunc> setParentMenuName(List<SysFunc> list) {
        if (list != null && list.size() > 0) {
            for (SysFunc sysFunc : list) {
                //找到二级菜单的父菜单 给parentMenuName赋值
                SysMenu menuParent = sysMenuMapper.selectByPrimaryKey(sysFunc.getMenu().getMenuParentId());
                sysFunc.setParentMenuName(menuParent.getMenuName());
            }
        }

        return list;
    }



    @Transactional
	@Override
	public void updateFunc(SysFunc sysFunc) {
		Date date = new Date();
		sysFunc.setUpdateTime(date);
		sysFunc.setUpdateUser("admin");
		sysFunc.setStatus(0);
		sysFuncMapper.updateByPrimaryKey(sysFunc);
		sysFunc.setCreatedTime(date);
		sysFunc.setCreatedUser("admin");
		sysFunc.setUpdateTime(null);
		sysFunc.setUpdateUser("");
		sysFunc.setStatus(1);
		sysFuncMapper.insert(sysFunc);
	}
*/
}
